﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability.API.Applications.Command.SlaughterCommand;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrastructure;

namespace Traceability.API.Applications.CommandHandler.SlaughterCommandHandler
{
    public class SlaughterHouseQueryCommandHandler : IRequestHandler<SlaughterHouseQueryCommand, APIResult<Slaughter_House>>
    {
        private readonly IBaseRepository<Slaughter_House> houseRepository;

        public SlaughterHouseQueryCommandHandler(IBaseRepository<Slaughter_House> houseRepository)
        {
            this.houseRepository = houseRepository;
        }

        public Task<APIResult<Slaughter_House>> Handle(SlaughterHouseQueryCommand request, CancellationToken cancellationToken)
        {
            var res = houseRepository.GetAll().AsNoTracking().FirstOrDefault(s => !s.IsDeltede && s.Batch == request.Batch);
            APIResult<Slaughter_House> aPIResult = new APIResult<Slaughter_House>();
            if (res == null)
            {
                aPIResult.Code=EnumCode.File;
                aPIResult.Msg = "显示失败";
            }
            else
            {
                aPIResult.Code = EnumCode.Ok;
                aPIResult.Msg = "显示成功";
                aPIResult.Data = res;
            }

            return Task.FromResult(aPIResult);
        }
    }
}
